<%--
  Created by IntelliJ IDEA.
  User: JD
  Date: 2023/9/19
  Time: 19:58
  To change this template use File | Settings | File Templates.
--%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="<%=basePath%>static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="<%=basePath%>static/css/font.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/xadmin.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap.css">
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/jqPaginator.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/xadmin.js"></script>
<%--    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3.3.7/css/bootstrap.min.css">--%>
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">订单列表</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input type="text" placeholder="请输入用户名" id="queryUser" autocomplete="off" class="layui-input">
            <button class="layui-btn" type="button" onclick="selectUser()"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>

    <xblock>
        <button class="layui-btn" onclick="x_admin_show('添加信息','/sys/goOrderadd',500,450)"><i class="layui-icon"></i>添加
        </button>
        <span class="x-right" style="line-height:40px" id="showTotal">共有数据：1 条</span>
    </xblock>
<table class="layui-table">
    <thead>
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>电话</td>
        <td>票名</td>
        <td>票余量</td>
        <td>票单价</td>
        <td>下单时间</td>
        <td>购票数量</td>
        <td>订单价格</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody id="tab"></tbody>
    <tr>
        <td colspan="10" align="center">
            <ul class="pagination" id="pagination1"></ul>
        </td>
    </tr>
</table>
</div>

<script type="text/javascript" src="<%=basePath%>/static/lib/layui/__unpkg.com_layui@2.7.6_dist_layui.js"></script>
<script type="text/javascript">
    //弹出消息框，点击确定才删除用户
    layui.use(function(){
        var layer = layui.layer;
        var util = layui.util;
        // 事件
        util.on('lay-on', {
            "deleteOrder": function(){
                if(localStorage.getItem("role") == -1){
                    location.href = "../sys/goUnauthorized";
                    return;
                }
                var orderid = $(this).attr("param-data"); // 获取按钮的自定义属性值
                layer.confirm('删除后不可恢复，是否确定删除？', {icon: 0}, function(){
                    console.log(orderid);
                    $.ajax({
                        url: "../order/deleteOrder?orderid=" + orderid,
                        type:"get",
                        success: function (data){
                            layer.msg('删除用户成功! ', {icon: 1});
                            setTimeout(function(){
                                location.reload();
                            }, 500); // 延迟0.5秒后刷新页面
                        },
                        error: function (data){
                            layer.msg('删除用户失败！', {icon: 2});
                        }
                    });
                }, function(){
                    layer.msg('取消删除');
                });
            }
        })
    });
    //解决enter键问题
    $(document).ready(function enterJudge(e) {
        $(this).keydown(function (e) {
            if(e.which == "13" && $("#queryUser").val()!=''){
                selectUser();
                return false;
            }
        })
    })
    //总页数
    let totalPages=0;
    //总条数
    let totalCounts=0;
    //当前页
    let currentPage=0;
    //页面一加载后就执行，这里就是查询信息
    $(document).ready(function () {
        //执行全查
        selectAll(1);
        // 加载按钮
        $("#pagination1").jqPaginator({
            //总页数
            totalPages:totalPages,
            //总条数
            totalCounts:totalCounts,
            //当前页
            currentPage:currentPage,
            //加载按钮
            first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
            prev:'<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
            next:'<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last:'<li class="last"><a href="javascript:void(0);">末页</a></li>',
            page:'<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (number) {
                //当页面以改变就执行的函数
                selectAll(number);
            }
        })
    })
    //全查
    function selectAll(number) {
        //做ajax请求
        $.ajax({
            url:"../order/getOrder",
            dataType:"json",
            type:"post",
            async:false,
            data:{
                pageNum:number,
                pageSize:5,
                username:$("#queryUser").val(),
            },success:function (data) {
                console.log(data);
                //取出返回数据中用户 数据
                let userList = data.orderPageInfo.list;
                //判断userList是否有数据
                if(userList.length>0){
                    totalPages=data.orderPageInfo.pages;  //总页数
                    totalCounts=data.orderPageInfo.total; //总条数
                    currentPage=data.orderPageInfo.pageNum;//当前页
                    //把数据写在页面上
                    let res= ''; //用res变量拼接字符串
                    let count = 1;  //展示的编号
                    for(let i=0;i<userList.length;i++){
                        // 拿到每个人对应的订单列表
                        let orderList = userList[i].orders;
                        for(let j=0;j<orderList.length;j++){
                            // 拿到对应的票表
                            let ticketList = orderList[j].tickets;
                            let tempstr = "";
                            for(let k=0;k<ticketList.length;k++,count++){
                                tempstr +='<tr>'+
                                    '<td>'+count+'</td>'+
                                    '<td>'+userList[i].username+'</td>'+
                                    '<td>'+userList[i].phone+'</td>' +
                                    '<td>'+ticketList[k].ticketname+'</td>'+
                                    '<td>'+ticketList[k].ticketnum+'</td>'+
                                    '<td>'+ticketList[k].price+'</td>'+
                                    '<td>'+
                                        layui.util.toDateString(orderList[j].paymenttime, "yyyy-MM-dd HH:mm") +
                                    '</td>'+
                                    '<td>'+orderList[j].num+'</td>'+
                                    '<td>'+orderList[j].sumprice+'</td>'+
                                    '<td class="td-manage">' +
                                        '<button type="button" onclick="x_admin_show(\'用户修改\', \'/order/showOrder?orderid=' + orderList[j].orderid + '\', 455, 435)"><i class="layui-icon">&#xe642;</button>&emsp;'+
                                        '<button type="button" id="deleteBtn" param-data="'+ orderList[j].orderid +'" lay-on="deleteOrder"><i class="layui-icon">&#xe640;</i></button>' +
                                    '</td>'+
                                    '</tr>'
                            }
                            res +=tempstr;
                        }
                    }
                    $("#tab").html(res);
                }else {//没有数据
                    totalPages=1;
                    totalCounts=1;
                    currentPage=1;
                    $("#tab").html('<tr><td colspan="8" align="center">没有订单数据请添加数据</td></tr>');
                }
            },error:function () {
                alert("查询订单服务器错误");
            }
        })
    }
    //模糊查询用户
    function selectUser() {
        //初始化
        selectAll(1);
        //加载数据
        $("#pagination1").jqPaginator({
            totalPages:totalPages,
            totalCounts:totalCounts,
            currentPage:currentPage,
            //加载按钮
            first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
            prev:'<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
            next:'<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last:'<li class="last"><a href="javascript:void(0);">末页</a></li>',
            page:'<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (number) {
                //当页面以改变就执行的函数
                selectAll(number);
            }
        })
    }
</script>
</body>
</html>
